package com.hsq.jdbc.mysql;



import com.mysql.cj.jdbc.Driver;
import com.sun.javaws.IconUtil;
import org.junit.jupiter.api.Test;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

public class Jdbc01 {
    public static void main(String[] args) throws SQLException {
        //1、与数据库建立连接
        //注册驱动
        Driver driver = new Driver();
        //2.得到连接
        //mysql的连接本质就是之前学过的stock
        String url = "jdbc:mysql://localhost:3306/db_work";
        //将用户名和密码放入Properties对象中
        Properties properties = new Properties();
        //user和password是规定好配的，后面根据自己登入的用户实际填写
        properties.setProperty("user", "root");
        properties.setProperty("password", "Root123456");
        Connection connect = driver.connect(url, properties);
        //3.执行sql语句
        String sql = "insert into t_person values('刘德华', 40, '男')";
//        String sql = "update t_person set name = '周星驰' where name = '刘德华'";
//        String sql = "delete from t_person where name = '张三'";
        //用于执行静态SQL语句并返回其生成结果的对象
        Statement statement = connect.createStatement();
        int i = statement.executeUpdate(sql);//如果是dml语句，则返回影响的行数
        System.out.println(i > 0 ? "成功" : "失败");
        //4,关闭连接
        statement.close();
        connect.close();
    }

    @Test
    public void connect02() throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {
        //使用反射加载Driver类，动态加载，灵活性高，减少依赖性
        Class<?> aclass = Class.forName("com.mysql.cj.jdbc.Driver");
        Driver driver = (Driver) aclass.newInstance();
        String url = "jdbc:mysql://localhost:3306/db_work";
        Properties properties = new Properties();
        properties.setProperty("user", "root");
        properties.setProperty("password", "Root123456");
        Connection connection = driver.connect(url, properties);
        System.out.println("Method2 = " + connection);
        connection.close();
    }

    @Test
    public void connect03() throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {
        Class<?> aclass = Class.forName("com.mysql.cj.jdbc.Driver");
        Driver driver = (Driver) aclass.newInstance();
        String url = "jdbc:mysql://localhost:3306/db_work";
        String user = "root";
        String password = "Root123456";

        DriverManager.registerDriver(driver);
        Connection connection = DriverManager.getConnection(url, user, password);
        System.out.println("Method3 = " + connection);
        connection.close();
    }

    @Test
    //使用Class.forName自动完成注册驱动，简化代码
    //该种方式获取连接使用最多，推荐使用
    public void connect04() throws ClassNotFoundException, SQLException {
        //在加载Driver类时就完成注册了static {
        //        try {
        //            DriverManager.registerDriver(new Driver());
        //        } catch (SQLException var1) {
        //            throw new RuntimeException("Can't register driver!");
        //        }
        //    }
//        jdk1.5后使用了jdbc4，可以不用这个注册，程序会自动注册，但是建议写一下，因为这样更加明确
        Class.forName("com.mysql.cj.jdbc.Driver");

        String url = "jdbc:mysql://localhost:3306/db_work";
        String user = "root";
        String password = "Root123456";
        Connection connection = DriverManager.getConnection(url, user, password);
        System.out.println("Method4 = " + connection);
        connection.close();
    }

    @Test
    //在方式4上改进，即增加配置文件，让廉洁mysql数据库更加灵活，一般使用这个方式5
    public void connect05() throws IOException, ClassNotFoundException, SQLException {
        Properties properties = new Properties();
        properties.load(new FileInputStream("src\\mysql.properties"));
        String user = properties.getProperty("user");
        String password = properties.getProperty("password");
        String driver = properties.getProperty("driver");
        String url = properties.getProperty("url");

        Class.forName(driver);
        Connection connection = DriverManager.getConnection(url, user, password);
        System.out.println("Method5 = " + connection);
        connection.close();
    }
}
